import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpServer3 {
    public static void main(String[] args) throws IOException {
        ExecutorService executorService = Executors.newFixedThreadPool(
                Runtime.getRuntime().availableProcessors() * 4
        );
        final ServerSocket serverSocket = new ServerSocket(8083);
        while (true){
            try {
                final Socket accept = serverSocket.accept();
                executorService.execute(()->service(accept));
            }catch (Exception e){
                e.printStackTrace();
            }

        }
    }

    private static void service(Socket accept) {
        try {
            PrintWriter printWriter = new PrintWriter(accept.getOutputStream(),true);
            printWriter.println("HTTP/1.1 200 OK");
            printWriter.println("Content-Type:text/html;charset:utf-8");
            String body = "Hello NIO-3";
            printWriter.println("Content-length:"+body.getBytes().length);
            printWriter.println();
            printWriter.write(body);
            printWriter.close();
            accept.close();
        }catch (Exception exception){
            exception.printStackTrace();
        }

    }
}
